<template>
  <el-dialog
    title="分配角色"
    :visible="show"
    center
    width="40%"
    @close="closeFn"
  >

    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.title }}</el-checkbox>
    </el-checkbox-group>

    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="comfirmFn">提交</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>

</template>

<script>

import { getRoleList, getEmployeesById, editEmpolyee } from '@/api/employees'

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    userId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {

      checkList: [],
      roleList: []

    }
  },
  async created() {
    this.roleList = await getRoleList({
      _limit: 1000,
      _start: 0
    })
    console.log(57, this.roleList)
  },
  methods: {
    // 回显数据
    async  getEmployeesById(id) {
      const res = await getEmployeesById(id)
      this.checkList = res.roles
      this.checkList = this.checkList.map(item => {
        return item.id
      })
    },
    // 点击确认，通过校验，获取编辑接口，提示信息，通知父组件调用，关闭弹框
    async comfirmFn() {
      await editEmpolyee(this.userId, {
        roles: this.checkList
      })
      this.$message.success('分配角色成功')
      this.$emit('editEmployee')
      this.closeFn()
    },
    // 关闭弹框
    closeFn() {
      this.$emit('update:show', false)
      this.checkList = []
    }
  }

}
</script>

<style lang="scss" >

.el-dialog__header {
    background: #66b1ff;

    .el-dialog__title{
       color: #fff;
    }

}
.el-dialog__body{
    margin-left: 100px;
}
h4{
  margin-left: 44px;
}
</style>
